package com.balabala.gym_goods.controller.admin;

import cn.hutool.core.date.DateUtil;
import com.balabala.gym_goods.pojo.dto.AddCoachScheduleDto;
import com.balabala.gym_goods.pojo.dto.CoachScheduleDto;
import com.balabala.gym_goods.pojo.dto.PrivateCoursePageDto;
import com.balabala.gym_goods.pojo.entity.CoachSchedule;
import com.balabala.gym_goods.pojo.entity.Course;
import com.balabala.gym_goods.service.CourseService;
import com.balabala.gym_system.api.AdminFeignClient;
import com.balabala.gym_system.pojo.vo.AdminVo;
import com.balabala.result.Result;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.simpleframework.xml.Path;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Author 何涛
 * @Date: 2023/05/10/ 19:13
 * @description 私教课程控制器
 */
@RestController
@RequestMapping("/admin/privateCourse")
@Slf4j
@CrossOrigin
@Api(tags = "私教课程控制器")
public class PrivateCourseController {
    @Autowired
    CourseService courseService;
    @Autowired
    AdminFeignClient adminFeignClient;
    @PostMapping("savePrivateCourse")
    @ApiOperation("添加私教课程")
    public Result savePrivateCourse(@RequestBody Course course)
    {
        course.setCourseType(2);
        return Result.judge(courseService.save(course));
    }

    @DeleteMapping("deleteById/{id}")
    @ApiOperation("删除私教课程")
    public Result deleteById(@PathVariable Long id)
    {
        return Result.judge(courseService.removeById(id));
    }

    @GetMapping("findAll")
    @ApiOperation("查询所有")
    public  Result findAll()
    {
        return Result.ok(courseService.list(new LambdaQueryWrapper<Course>().eq(Course::getCourseType,2)));
    }

    @PostMapping("addPrivateCourseSchedule")
    @ApiOperation(value = "添加私教课程排课信息")
    public Result addPrivateCourseSchedule(@RequestBody AddCoachScheduleDto addCoachScheduleDto)
    {

        return Result.judge(courseService.addPrivateCourseSchedule(addCoachScheduleDto));
    }

    @ApiOperation("根据id查询私教信息")
    @GetMapping("getPrivateCourseById/{courseId}")
    public Result getPrivateCourseById(@PathVariable Long courseId)
    {
        return Result.ok(courseService.list(new LambdaQueryWrapper<Course>().eq(Course::getId,courseId).eq(Course::getCourseType,2)));
    }

    @PutMapping("updatePrivateCourse")
    @ApiOperation("修改私教课程")
    public Result updatePrivateCourse(@RequestBody Course course){
        return Result.judge(courseService.updateById(course));
    }

    @ApiOperation("查看私教课程分页信息")
    @PostMapping("getPrivateCoursePageList")
    public Result getPrivateCoursePageList(@RequestBody PrivateCoursePageDto privateCoursePageDto){
        PageHelper.startPage(privateCoursePageDto.getPage(),privateCoursePageDto.getLimit());
        LambdaQueryWrapper<Course> courseLambdaQueryWrapper = new LambdaQueryWrapper<>();
        if(!StringUtils.isEmpty(privateCoursePageDto.getCourseName())){
            courseLambdaQueryWrapper.like(Course::getName,privateCoursePageDto.getCourseName());
        }
        if(privateCoursePageDto.getCoachId()!=null&&privateCoursePageDto.getCoachId()!=0){
            courseLambdaQueryWrapper.eq(Course::getCoachId,privateCoursePageDto.getCoachId());
        }
        courseLambdaQueryWrapper.eq(Course::getCourseType,2);
        PageInfo<Course> coursePageInfo = new PageInfo<>(courseService.list(courseLambdaQueryWrapper));
        List<Course> courses = coursePageInfo.getList();
        courses.forEach(course -> {
            if(course.getCoachId()!=null){
                // 远程调用获取教练名称
                AdminVo data = adminFeignClient.findById(course.getCoachId()).getData();
                if(data!=null){
                    course.setCoachName(data.getName());
                }
            }

        });
        return Result.ok(courses,coursePageInfo.getTotal());
    }



}
